package io.renren.modules.generator.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.generator.controller.vo.EnterpriseSupervisionDealVo;
import io.renren.modules.generator.controller.vo.EnterpriseSupervisionReportVo;
import io.renren.modules.generator.controller.vo.EnterpriseSupervisionVerifyVo;
import io.renren.modules.generator.dao.EnterpriseMainDao;
import io.renren.modules.generator.dao.EnterpriseSupervisionFlowDao;
import io.renren.modules.generator.dao.EnterpriseSupervisionRecordDao;
import io.renren.modules.generator.entity.EnterpriseMainEntity;
import io.renren.modules.generator.entity.EnterpriseSupervisionFlowEntity;
import io.renren.modules.generator.entity.EnterpriseSupervisionRecordEntity;
import io.renren.modules.generator.entity.EnterpriseSupervisionRecordInfoEntity;
import io.renren.modules.generator.service.EnterpriseMainService;
import io.renren.modules.generator.service.EnterpriseSupervisionRecordService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author zhengd
 */
@Slf4j
@Service("enterpriseSupervisionRecordService")
public class EnterpriseSupervisionRecordServiceImpl extends ServiceImpl<EnterpriseSupervisionRecordDao, EnterpriseSupervisionRecordEntity> implements EnterpriseSupervisionRecordService {

    @Autowired
    private EnterpriseSupervisionFlowDao enterpriseSupervisionFlowDao;

    @Autowired
    private EnterpriseMainService enterpriseMainService;

    @Autowired
    private EnterpriseMainDao enterpriseMainDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params,int roleType,int type,int checkType,String keyword) {
        String deptId = params.get("deptId").toString();
        QueryWrapper queryWrapper = new QueryWrapper();
        String lastsql = processPageSql(roleType,type,checkType,keyword,deptId);
        queryWrapper.last(lastsql);
        IPage<EnterpriseSupervisionRecordEntity> page = this.page(
                new Query<EnterpriseSupervisionRecordEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }

    @Override
    public EnterpriseSupervisionRecordInfoEntity info(String id) {
        return this.baseMapper.info(id);
    }

    @Override
    public void report(EnterpriseSupervisionReportVo vo) {
        //组装对象
        EnterpriseSupervisionRecordEntity enterpriseSupervisionRecordEntity = new EnterpriseSupervisionRecordEntity();
        enterpriseSupervisionRecordEntity.setEnterpriseCode(vo.getCode());
        enterpriseSupervisionRecordEntity.setEnterpriseName(vo.getName());
        enterpriseSupervisionRecordEntity.setEventTitle(vo.getTitle());
        enterpriseSupervisionRecordEntity.setEventDesc(vo.getDesc());
        enterpriseSupervisionRecordEntity.setEventType(Integer.parseInt(vo.getType()));
        enterpriseSupervisionRecordEntity.setCheckType(Integer.parseInt(vo.getCheckType()));
        enterpriseSupervisionRecordEntity.setCurrentFlowStatus(1);
        enterpriseSupervisionRecordEntity.setCurrentRoleType(vo.getRoleType());
        enterpriseSupervisionRecordEntity.setCreateUserId(vo.getUserNo());
        enterpriseSupervisionRecordEntity.setCreateUserName(vo.getUserName());
        getBaseMapper().insert(enterpriseSupervisionRecordEntity);
        EnterpriseSupervisionFlowEntity enterpriseSupervisionFlowEntity = new EnterpriseSupervisionFlowEntity();
        enterpriseSupervisionFlowEntity.setRecordId(enterpriseSupervisionRecordEntity.getId() + "");
        enterpriseSupervisionFlowEntity.setPic(vo.getPics());
        enterpriseSupervisionFlowEntity.setLon(vo.getLon());
        enterpriseSupervisionFlowEntity.setLat(vo.getLat());
        enterpriseSupervisionFlowEntity.setAddress(vo.getAddress());
        enterpriseSupervisionFlowEntity.setProcessStatus(0);
        enterpriseSupervisionFlowEntity.setStatus(1);
        enterpriseSupervisionFlowEntity.setSort(1);
        enterpriseSupervisionFlowEntity.setRoleType(vo.getRoleType());
        enterpriseSupervisionFlowEntity.setCreateUserId(vo.getUserNo());
        enterpriseSupervisionFlowEntity.setCreateUserName(vo.getUserName());
        getEnterpriseSupervisionFlowDao().insert(enterpriseSupervisionFlowEntity);
        //更新写入基本表当前流程ID
        enterpriseSupervisionRecordEntity.setCurrentFlowId(enterpriseSupervisionFlowEntity.getId());
        getBaseMapper().updateById(enterpriseSupervisionRecordEntity);
    }

    @Override
    public void deal(EnterpriseSupervisionDealVo vo) {
        EnterpriseSupervisionFlowEntity enterpriseSupervisionFlowEntity = new EnterpriseSupervisionFlowEntity();
        enterpriseSupervisionFlowEntity.setRecordId(vo.getId() + "");
        enterpriseSupervisionFlowEntity.setPic(vo.getPics());
        enterpriseSupervisionFlowEntity.setLon(vo.getLon());
        enterpriseSupervisionFlowEntity.setLat(vo.getLat());
        enterpriseSupervisionFlowEntity.setAddress(vo.getAddress());
        enterpriseSupervisionFlowEntity.setProcessStatus(1);
        enterpriseSupervisionFlowEntity.setStatus(2);
        //查询排序数量
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("record_id",vo.getId());
        int count = getEnterpriseSupervisionFlowDao().selectCount(queryWrapper);
        enterpriseSupervisionFlowEntity.setSort(count + 1);
        enterpriseSupervisionFlowEntity.setRemark(vo.getRemark() == null ? "" : vo.getRemark());
        enterpriseSupervisionFlowEntity.setRoleType(vo.getRoleType());
        enterpriseSupervisionFlowEntity.setCreateUserId(vo.getUserNo());
        enterpriseSupervisionFlowEntity.setCreateUserName(vo.getUserName());
        getEnterpriseSupervisionFlowDao().insert(enterpriseSupervisionFlowEntity);
        //开始更新主流程事件状态
        EnterpriseSupervisionRecordEntity enterpriseSupervisionRecordEntity = new EnterpriseSupervisionRecordEntity();
        enterpriseSupervisionRecordEntity.setCurrentFlowId(enterpriseSupervisionFlowEntity.getId());
        enterpriseSupervisionRecordEntity.setCurrentFlowStatus(2);
        enterpriseSupervisionRecordEntity.setCurrentRoleType(vo.getRoleType());
        enterpriseSupervisionRecordEntity.setId(Integer.parseInt(vo.getId()));
        enterpriseSupervisionRecordEntity.setUpdateTime(new Date());
        enterpriseSupervisionRecordEntity.setUpdateUserId(vo.getUserNo());
        enterpriseSupervisionRecordEntity.setUpdateUserName(vo.getUserName());
        getBaseMapper().updateById(enterpriseSupervisionRecordEntity);
    }

    @Override
    public void verify(EnterpriseSupervisionVerifyVo vo) {
        int roleType = vo.getRoleType();
        int processStatus = Integer.parseInt(vo.getProcessStatus());
        EnterpriseSupervisionFlowEntity enterpriseSupervisionFlowEntity = new EnterpriseSupervisionFlowEntity();
        enterpriseSupervisionFlowEntity.setRecordId(vo.getId() + "");
        enterpriseSupervisionFlowEntity.setPic(vo.getPics());
        enterpriseSupervisionFlowEntity.setLon(vo.getLon());
        enterpriseSupervisionFlowEntity.setLat(vo.getLat());
        enterpriseSupervisionFlowEntity.setAddress(vo.getAddress());
        enterpriseSupervisionFlowEntity.setProcessStatus(processStatus);
        if (processStatus == 0) {
            enterpriseSupervisionFlowEntity.setStatus(5);
        } else {
            if (roleType == 2) {
                enterpriseSupervisionFlowEntity.setStatus(3);
            } else {
                enterpriseSupervisionFlowEntity.setStatus(4);
            }
        }
        //查询排序数量
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("record_id",vo.getId());
        int count = getEnterpriseSupervisionFlowDao().selectCount(queryWrapper);
        enterpriseSupervisionFlowEntity.setSort(count + 1);
        enterpriseSupervisionFlowEntity.setRemark(vo.getRemark());
        enterpriseSupervisionFlowEntity.setRoleType(roleType);
        enterpriseSupervisionFlowEntity.setCreateUserId(vo.getUserNo());
        enterpriseSupervisionFlowEntity.setCreateUserName(vo.getUserName());
        getEnterpriseSupervisionFlowDao().insert(enterpriseSupervisionFlowEntity);
        //开始更新主流程事件状态
        EnterpriseSupervisionRecordEntity enterpriseSupervisionRecordEntity = new EnterpriseSupervisionRecordEntity();
        enterpriseSupervisionRecordEntity.setCurrentFlowId(enterpriseSupervisionFlowEntity.getId());
        enterpriseSupervisionRecordEntity.setCurrentFlowStatus(enterpriseSupervisionFlowEntity.getStatus());
        enterpriseSupervisionRecordEntity.setCurrentRoleType(roleType);
        enterpriseSupervisionRecordEntity.setId(Integer.parseInt(vo.getId()));
        enterpriseSupervisionRecordEntity.setUpdateTime(new Date());
        enterpriseSupervisionRecordEntity.setUpdateUserId(vo.getUserNo());
        enterpriseSupervisionRecordEntity.setUpdateUserName(vo.getUserName());
        getBaseMapper().updateById(enterpriseSupervisionRecordEntity);
    }


    private String processPageSql (int roleType,int type,int checkType,String keyword,String deptId) {
        StringBuffer lastsql = new StringBuffer();
        List<String>  list = enterpriseMainDao.getEnterpriseList(deptId, null);
        lastsql.append(" where check_type = ").append(checkType);
        if (type == 1) {
            if (roleType == 1) {
                //安全员待处理
                EnterpriseMainEntity enterpriseMainEntity = enterpriseMainService.getByDeptid(Integer.parseInt(deptId));
                lastsql.append(" and (current_flow_status = 1 or current_flow_status = 5) ").append(" and enterprise_code = '").append(enterpriseMainEntity.getId()).append("'");
            } else if (roleType == 2) {
                lastsql.append(" and current_flow_status = 2");
            } else {
                lastsql.append(" and (current_flow_status = 2 or current_flow_status = 3)");
            }
        } else if (type == 2) {
            if (roleType == 1) {
                lastsql.append(" and (current_flow_status = 2 or current_flow_status = 3)").append(" and enterprise_code in (").append(list.stream().collect(Collectors.joining(","))).append(")");
            } else {
                lastsql.append(" and (current_flow_status = 2 or current_flow_status = 3)");
            }
        } else if (type == 3){
            lastsql.append(" and current_flow_status = 4");
            if (roleType == 1) {
                lastsql.append(" and enterprise_code in (").append(list.stream().collect(Collectors.joining(","))).append(")");
            }
        }
        if (StringUtils.isNotBlank(keyword)) {
            if (StringUtils.isNotBlank(lastsql.toString())) {
                lastsql.append(" and");
            }
            lastsql.append(" (enterprise_name like '%").append(keyword).append("%'")
                    .append(" or event_title like '%").append(keyword).append("%'")
                    .append(" or event_desc like '%").append(keyword).append("%'")
                    .append(")");
        }
        lastsql.append(" ORDER BY update_time DESC,create_time DESC,id DESC");
        return lastsql.toString();
    }

    public EnterpriseSupervisionFlowDao getEnterpriseSupervisionFlowDao() {
        return enterpriseSupervisionFlowDao;
    }
}
